//
//  FoodDetailViewController.m
//  shise
//
//  Created by 毛志鹏 on 14/12/17.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "FoodDetailViewController.h"
#import "Food.h"
#import "FoodCell.h"
#import "NetHandler.h"
#import "FoodDetailPicCell.h"
#import "UIImageView+WebCache.h"

@interface FoodDetailViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIButton *button;

@property (nonatomic, retain) NSMutableArray *foodArr;

@property (nonatomic, retain) NSString *pic_url;
@property (nonatomic, retain) NSString *userHead_url;
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSString *timeInfo;
@property (nonatomic, retain) NSString *content;


@end

@implementation FoodDetailViewController

// 处理数据的方法
- (void)handleData
{
//    NSString *str = [NSString stringWithFormat:@"https://api.shiseapp.com%@",self.food.objId];
//    NSLog(@"%@", str);
    
        NSString *str = [NSString stringWithFormat:@"https://api.shiseapp.com%@",self.foodApi];
    
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSError *resultError = nil;
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&resultError];
        
        NSArray *arr = [result objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {
            if ([[tempDic objectForKey:@"type"] isEqualToString:@"ImageCard"]) {
                
                self.pic_url = [[tempDic objectForKey:@"pic"] objectForKey:@"url"];
                
            } else if ([[tempDic objectForKey:@"type"] isEqualToString:@"UserCard"]) {
                
                self.userHead_url = [[tempDic objectForKey:@"user"] objectForKey:@"avatar"];
                self.userName = [tempDic objectForKey:@"title"];
                self.timeInfo = [tempDic objectForKey:@"desc"];
                
            } else if ([[tempDic objectForKey:@"type"] isEqualToString:@"ContentCard"]) {
                
                self.content = [tempDic objectForKey:@"content"];
            }
        }

        [self.tableView reloadData];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 350;
    } else if (indexPath.row == 1) {
        return 40;
    } else {
        return 60;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FoodDetailPicCell *foodPicCell = [tableView dequeueReusableCellWithIdentifier:@"foodpic"];
        if (foodPicCell == nil) {
            foodPicCell = [[[FoodDetailPicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"foodpic"]autorelease];
        }
        [foodPicCell.pic setImageWithURL:[NSURL URLWithString:self.pic_url]];
        return foodPicCell;
    } else if (indexPath.row == 1) {
        UITableViewCell *userCell = [tableView dequeueReusableCellWithIdentifier:@"user"];
        if (userCell == nil) {
            userCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"user"]autorelease];
        }
        userCell.imageView.layer.cornerRadius = 15;
        [userCell.imageView setImageWithURL:[NSURL URLWithString:self.userHead_url]];
        userCell.textLabel.text = self.userName;
        userCell.detailTextLabel.text = self.timeInfo;
        
        return userCell;
    } else {
        UITableViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"content"];
        if (contentCell== nil) {
            contentCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"content"]autorelease];
        }
        contentCell.textLabel.font = [UIFont boldSystemFontOfSize:14];
        contentCell.textLabel.text = self.content;
        contentCell.textLabel.numberOfLines = 0;
        return contentCell;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"美食详情";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.button setFrame:CGRectMake(0, self.view.frame.size.height - 30, self.view.frame.size.width, 30)];
    self.button.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.button];
    
    
    
    self.hidesBottomBarWhenPushed = YES;
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
//    [self followRollingScrollView:self.tableView];
    
    
    
    
    
    [self handleData];
    
    
    // Do any additional setup after loading the view.
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
